package cn.xdwanj.ask.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.xdwanj.ask.service.AskclassService;
import cn.xdwanj.ask.service.AsklistService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xdwanj
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ajax")
public class MainController {

    private final AsklistService asklistService;
    private final AskclassService askclassService;

    @RequestMapping("/test.do")
    public String test() {
        return "123456";
    }

    @RequestMapping("/test2.do")
    public String test2() {
        return "123456";
    }

    @RequestMapping("/getAsks.do")
    public List<Map<String, Object>> getAskList(@RequestParam(defaultValue = "1") int num) {
        var asklistList = asklistService.list();

        return asklistList.stream()
            .map(BeanUtil::beanToMap)
            .peek(it -> {
                var askclass = askclassService.getById((Integer) it.get("class"));
                if (askclass == null) {
                    return;
                }
                it.put("classname", askclass.getName());
                it.put("classcolor", askclass.getColor());
            }).collect(Collectors.toList());

    }
}
